﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DATA
{
    public class ProductTypeREP
    {

        MODELGENERALDataContext context = new MODELGENERALDataContext();


        public List<ProductType> GetProductTypes()
        {
            List<ProductType> acc = new List<ProductType>();

            var acounts = from a in context.ProductTypes
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public List<ProductType> GetActiveProductType()
        {
            List<ProductType> acc = new List<ProductType>();

            var acounts = from a in context.ProductTypes

                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public bool CheckDuplicate(int AccountID)
        {
            ProductType newacc = new ProductType();

            for (int i = 0; i < (int)(context.ProductTypes.Count()); i++)
            {
                ProductType acc = context.ProductTypes.Single(c => c == newacc);
                if (AccountID.Equals(acc.typeID))
                {
                    return true;
                }




            }
            return false;
        }

        public bool AddProductType(ProductType newAccount)
        {
            ProductType newacc = new ProductType();
            ProductType acc = context.ProductTypes.Single(c => c == newacc);
            if (CheckDuplicate(newAccount.typeID) == false)
            {
                context.ProductTypes.InsertOnSubmit(acc);

                context.SubmitChanges();
            }

            return true;

        }
        public void deleteProductType(int AccountID)
        {
            var acc = from c in context.ProductTypes
                      where c.typeID == AccountID
                      select c;

            context.ProductTypes.DeleteAllOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdateProductType(ProductType accountup)
        {
            try
            {
                ProductType acc = context.ProductTypes.Single(
                                                        nv => nv.typeID == accountup.typeID
                                                        );
                acc.typeName = accountup.typeName;
                acc.isActived = accountup.isActived;
                acc.description = accountup.description;
              



                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
